package com.example.springbook.Controller;

import com.example.springbook.constant.Constants;
import com.example.springbook.model.Result;
import com.example.springbook.model.UserInfo;
import com.example.springbook.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login", produces = "application/json")
    public Result login(String userName, String password, HttpSession session){
        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){
            return Result.fail("用户名或密码为空");
        }
        UserInfo user = userService.getUserInfoByname(userName);
        if(user==null){
            return Result.fail("用户为空");
        }
        if(!password.equals(user.getPassword())){
            return Result.fail("密码错误");
        }
        session.setAttribute(Constants.USER_SESSION_KEY,user);
        UserInfo userInfo=(UserInfo)session.getAttribute(Constants.USER_SESSION_KEY);
        System.out.println(userInfo.toString());
        return Result.success("") ;
    }
}
